//
//  PPNetCategorys.m
//  PPNetworking
//
//  Created by ppwang on 2021/12/14.
//

#import "PPNetCategorys.h"
#import <CommonCrypto/CommonDigest.h>

@implementation NSDictionary (PPNetworking)

- (NSArray *)pp_allKeysSorted {
    return [[self allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
}

- (NSString *)pp_jsonStringEncoded {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return nil;
}


@end

@implementation NSString (PPNetworking)
/*!
 *  编译成MD5格式的字符串
 *
 *  @return MD5 string
 */
- (NSString *)pp_md5String {
    
    const char *cStr = [self UTF8String];
    unsigned char digest[16];
    CC_MD5( cStr, (CC_LONG)strlen(cStr), digest );
    
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [output appendFormat:@"%02x", digest[i]];
    
    return  output;
}

@end
